<?php
class reporterController extends  appController{
    
    public function reporterList()
    {
        $list = Model('reporter') -> get_reporter_list();
        $this->assign("list", $list);
        $this->display("reporter_list.html");
    }
    
    public function editReporter()
    {
        if(isset($_GET['id'])){
            $id = $_GET['id'];
            $info = Model('reporter')->get_reporter($id);
            $this->assign('info', $info);
        }
        $this->display("reporter_edit.html");
    }
    
    public function saveReporter()
    {
        $res = array('error' => 1, 'content' => '编辑失败');
        $m = Model('reporter');
        $info['group_id'] = 5;
        $info['username'] = yf_empty(trim($_POST['username']), "用户名不能为空");
        $info['password'] = yf_empty($_POST['new_password'], "密码不能为空");
		$confirm_password = yf_empty($_POST['confirm_password'], "确认密码不能为空");
	
		if($info['password'] != $confirm_password)
		{
			$res['content'] = Lang('twice_pwd_not_match');
			$this->showMsg($res);
		}
		
		if(strlen($info['password']) < 6)
		{
			$res['content'] = Lang('pwd_too_short');
			$this->showMsg($res);
		}
		$info['password'] = md5($info['password']);
		if(!$m -> check_info($info['username'],$_POST['id'])){
		    $res['content'] = "用户名已被使用";
		}else{
		    if(!empty($_POST['id'])){//编辑
		        $info['modified'] = yf_time();
		        $result = Model('reporter')->update($info,array('table'=>'admin','id'=>$_POST['id']));
		    }else{//新增
		        $info['created'] = yf_time();
		        $result = Model('reporter')->insert($info,'admin');
		        if($result){
    		        $arr['admin_id'] = $result;
    		        $reporter_id = Model('reporter') ->insert($arr,"reporter");
		        }
		    }
		    if($result){
		        $res['error'] = 0;
		        $res['content'] = "编辑成功";
		    }
		}
		$this->showMsg($res);
    }
    
    //记者信息
    public function myInfo()
    {
        $admin_id = intval(session("admin_id"));
        if(!$admin_id>0){
            $this->showMsg("请退出后重新登录");
        }
        $m = Model('reporter');
        if(!$m->is_reporter($admin_id)){
            $this->showMsg("抱歉，您不是记者，请用记者帐号登录");
        }
        $info = $m -> get_reporter_info($admin_id);
        $this->assign("info", $info);
        $this->display("my_info.html");
        
    }
    
    public function updateReporter()
    {
        $result = array('error' => 1, 'content' => '编辑失败');
        $id = $_POST['id'];
        $info['reporter_name'] = yf_empty($_POST['reporter_name'],"请输入记者名");
        $info['title'] = yf_empty($_POST['title'],"请输入首页标题");
        $info['home_desc'] = yf_empty($_POST['home_desc'],"请输入首页描述");
        $info['thumb_url'] = yf_empty($_POST['thumb_url'],"请上传首页缩略图");
        $info['reporter_url'] = yf_empty($_POST['reporter_url'],"请上传记者头像");
        $info['gratuity_url'] = yf_empty($_POST['gratuity_url'],"请上传您的打赏二维码");
        if(hy_check($info['title'],"","1-15")=="#@_error"){
            $result['content'] =  "首页标题字数最多15个字";
            $this->showMsg($result);
        }
        if(hy_check($info['home_desc'],"","1-50")=="#@_error"){
            $result['content'] =  "首页描述字数最多50个字";
            $this->showMsg($result);
        }
        $res = Model('reporter') -> update($info,array("table"=>"reporter","id"=>$id));
        if($res){
            $result['error'] = 0;
            $result['content'] = "编辑成功";
        }
        $this->showMsg($result);
    }
    
    public function editInfo()
    {
        $admin_id = intval(session("admin_id"));
        if(!$admin_id>0){
            $this->showMsg("请退出后重新登录");
        }
        $m = Model('reporter');
        if(!$m->is_reporter($admin_id)){
            $this->showMsg("抱歉，您不是记者，请用记者帐号登录");
        }
        $info = $m -> get_reporter_info($admin_id);
        $this->assign("info", $info);
        $this->display("edit_info.html");
    }
    
    public function editReporterNew()
    {
        $m = Model('reporter');
        if(!empty($_GET['id'])){
            $new_id = $_GET['id'];
            $info = $m -> get_new_info($new_id);
            $this->assign('info', $info);
        }
        $isLive = $m -> getLiveReporter($_REQUEST['reporterId']);
        $this->assign("reporterId", $_REQUEST['reporterId']);
        if($isLive>0){
            $this->display("edit_live.html");
        }else{
            $this->display("edit_new.html");
        }
    }
    
    public function updateNew()
    {
        $result = array('error' => 1, 'content' => '编辑失败');
        $id = $_POST['id'];
        $info['reporter_id'] = yf_empty($_POST['reporter_id'],"请重新进入页面进行编辑");
        $info['title'] = yf_empty($_POST['title'],"请输入新闻标题");
        $info['new_desc'] = yf_empty($_POST['new_desc'],"请输入新闻描述");
        $info['video_url'] = yf_empty($_POST['video_url'],"请上传视频路径");
        
        $info['tab_title'] = yf_empty($_POST['tab_title'],"请输入导航标题");
        if(hy_check($info['tab_title'],"","1-4")=="#@_error"){
            $result['content'] =  "导航标题字数最多4个字";
            $this->showMsg($result);
        }
        $info['createtime'] = strtotime(yf_empty($_POST['createtime'],"请选择新闻时间"));
        $info['status'] = yf_empty($_POST['status'],0,true);
        if($_POST['id']){
            $res = Model('reporter') -> update($info,array("table"=>"reporter_new","id"=>$id));
        }else{
            $res = Model('reporter') -> insert($info,"reporter_new");
            $id = $res;
            if($res){
                $url = "http://g4.airefp.com/ws/index.php?g=mobile&m=reporter&a=preview&new_id=$res";
                $qrCode = yf_qrcode2($url,6,true);
                $qrCode = str_replace(ROOT_PATH,"",$qrCode);
                $arr['preview_url'] = $qrCode;
                Model('reporter')->update($arr, array("table"=>"reporter_new","id"=>$res));
            }
        }
        if($res){
            if($info['status']>0){
                Model('reporter') -> updateStatus($info['status'],$id,$info['reporter_id']);
            }
            $result['error'] = 0;
            $result['content'] = "编辑成功";
        }
        $this->showMsg($result);
    }
    
    public function updateLive()
    {
        $result = array('error' => 1, 'content' => '编辑失败');
        $id = $_POST['id'];
        $info['reporter_id'] = yf_empty($_POST['reporter_id'],"请重新进入页面进行编辑");
        $info['title'] = yf_empty($_POST['title'],"请输入新闻标题");
        $info['new_desc'] = yf_empty($_POST['new_desc'],"请输入新闻描述");
        $info['createtime'] = strtotime(yf_empty($_POST['createtime'],"请选择新闻时间"));
        $info['type'] = yf_empty($_POST['type'],1,true);
        if($_POST['id']){
            $res = Model('reporter') -> update($info,array("table"=>"reporter_new","id"=>$id));
        }else{
            $res = Model('reporter') -> insert($info,"reporter_new");
            $id = $res;
            if($res){
                $url = "http://g4.airefp.com/ws/index.php?g=mobile&m=reporter&a=preview&new_id=$res";
                $qrCode = yf_qrcode2($url,6,true);
                $qrCode = str_replace(ROOT_PATH,"",$qrCode);
                $arr['preview_url'] = $qrCode;
                Model('reporter')->update($arr, array("table"=>"reporter_new","id"=>$res));
            }
        }
        if($res){
            $result['error'] = 0;
            $result['content'] = "编辑成功";
        }
        $this->showMsg($result);
    }
    
    public function newCommentList()
    {
        $m = Model('npc');
        $total['new_id'] = $_GET['new_id'];
        $total['startTime'] = yf_empty(strtotime($_GET['startTime']),"",true);
        $total['endTime'] = yf_empty(strtotime($_GET['endTime']),"",true);
        $total['nickname'] = yf_empty($_GET['nickname'],"",true);
        $total['comment'] = yf_empty($_GET['comment'],"",true);
        $total['status'] = yf_empty($_GET['status'],"all",true);
        $page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
        $list=Model('reporter')->listComment($page,$total);
        $this->assign('list',$list['data']);
        $this->assign('list_page',$list['pagelink']);
        $this->assign('page',$page);
        $this->assign("total", $total);
        $this->display("comment_list.html");
    }
    
    public function addDigest()
    {
        $id = isset($_POST['id'])?$_POST['id']:"";
        $res = Model('reporter')->addDigest($id);
        print_r(json_encode($res));
    }
    
    public function addLive()
    {
        $id = isset($_POST['id'])?$_POST['id']:"";
        $res = Model('reporter')->addLive($id);
        print_r(json_encode($res));
    }
    
    public function homeOrder()
    {
        $m = Model('reporter');
        $list = $m -> get_order();
        $this->assign("list", $list);
        $this->display("homeOrder.html");
    }
    
    public function ordering()
    {
        $result = array('error' => 1, 'content' => '编辑失败');
        $id = $_REQUEST['id'];
        if($id == "cha"){
            $info['reporter_place'] = yf_empty($_REQUEST['val'],0,true);
            $res = Model('reporter') -> update($info,array("table"=>"block_content","id"=>"76"));
        }else{
            $info['ordering'] = yf_empty($_REQUEST['val'],0,true);
            $res = Model('reporter') -> update($info,array("table"=>"reporter","id"=>$id));
        }
        if($res){
            $result['error'] = 0;
            $result['content'] = "编辑成功";
        }
        $this->showMsg($result);
    }
    
    public function reportCommentList()
    {
        $total['startTime'] = yf_empty(strtotime($_GET['startTime']),"",true);
        $total['endTime'] = yf_empty(strtotime($_GET['endTime']),"",true);
        $total['nickname'] = yf_empty($_GET['nickname'],"",true);
        $total['comment'] = yf_empty($_GET['comment'],"",true);
        $total['status'] = yf_empty($_GET['status'],"all",true);
        $total['reporter_name'] = yf_empty($_GET['reporter_name'],"",true);
        $page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
        $list=Model('reporter')->listComment2($page,$total);
        $this->assign('list',$list['data']);
        $this->assign('list_page',$list['pagelink']);
        $this->assign('page',$page);
        $this->assign("total", $total);
        $this->display("comment_list2.html");
    }
    
    public function uvpvList()
    {
        $total['startTime'] = yf_empty(strtotime($_GET['startTime']),"",true);
        $total['endTime'] = yf_empty(strtotime($_GET['endTime']),"",true);
        $total['reporter_name'] = yf_empty($_GET['reporter_name'],"",true);
        $page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
        $list=Model('reporter')->get_news($page,$total);
        $this->assign('list',$list['data']);
        $this->assign('list_page',$list['pagelink']);
        $this->assign('page',$page);
        $this->assign("total", $total);
        $this->display("uvpv.html");
    }
    
    public function getUVPV()
    {
        $sid = isset($_REQUEST['id'])?$_REQUEST['id']:"0";//week||regular||home
        $page = yf_empty($_REQUEST['page'], 1, true);
        $list = Model('reporter')->getUVPV($sid,$page);
        $this->assign("list", $list['list']);
        $this->assign("page", $list['page']);
         
        $this->assign("sid", $sid);
        $this->display("uvpv_list.html");
    }
    
    public function exportUVPV()
    {
        $sid = $_REQUEST['id'];
        $name = $_REQUEST['name'];
        $title = $_REQUEST['title'];
        $header = array("time","pv","uv");
        $result = Model('reporter')->getExportUVPV($sid);
        exportData($name."-".$title."UVPV",$header,$result);
    }
    
    public function detail()
    {
        $this->display("detail.html");
    }
    
    public function exportAllUVPV()
    {
        $reporter_name = yf_empty($_GET['reporter_name'],'',true);
        $uvpv = isset($_GET['uvpv'])?intval($_GET['uvpv']):1;
        $start = strtotime($_GET['start']);
        $end = strtotime($_GET['end']);
        $days = intval(($end-$start)/(24*60*60));
        $str = iconv('utf-8','gb2312',$uvpv=="1"?"新闻标题uv":"新闻标题pv");
        $str.= ",".iconv('utf-8','gb2312',"记者名");
        $times = array();
        for($i=0;$i<($days+1);$i++)
        {
            $time = date("Y-m-d",$start+(24*60*60*$i));
            $str.=",".$time;
            $times[] = strtotime($time);
        }
        $str.="\n";
        $result = Model('reporter')->getExport($reporter_name,$times,$uvpv);
        foreach ($result as $k=>$v)
        {
        foreach ($v as $kk=>$vv)
        {
        $str.=iconv('utf-8','gb2312',$vv).",";
        }
        $str = substr($str,0,strlen($str)-1);
            $str.="\n";
        }
        export_csv($uvpv=="1"?$reporter_name."uv".".csv":$reporter_name."pv".".csv",$str);

    }
}